package com.hnucm.cooklife.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.qmuiteam.qmui.widget.QMUITopBar;

import java.text.BreakIterator;

import xyz.sahildave.widget.SearchViewLayout;
import xyz.sahildave.widget.Utils;

public class MySearch extends SearchViewLayout {

    private int toolbarExpandedHeight = 0;
    private QMUITopBar mTopBar;
    private EditText mSearchEditText;
    private boolean mIsExpanded;
    private int mCollapsedHeight;

    public MySearch(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void toggleToolbar(boolean expanding) {
        if (this.mTopBar != null) {
            this.mTopBar.clearAnimation();
            if (expanding) {
                this.toolbarExpandedHeight = this.mTopBar.getHeight();
            }

            int toYValue = expanding ? this.toolbarExpandedHeight * -1 : 0;
            this.mTopBar.animate().y((float)toYValue).setDuration((long)ANIMATION_DURATION).start();
            Utils.animateHeight(this.mTopBar, expanding ? this.toolbarExpandedHeight : 0, expanding ? 0 : this.toolbarExpandedHeight, ANIMATION_DURATION);
        }
    }



    public void collapse() {
        this.toggleToolbar(false);
//        if (this.mBackgroundTransition != null) {
//            this.mBackgroundTransition.reverseTransition(ANIMATION_DURATION);
//        }

        this.mSearchEditText.setText((CharSequence)null);
        this.mIsExpanded = false;
//        this.animateStates(false, 0.0F, 1.0F);
//        Utils.crossFadeViews(this.mCollapsed, this.mExpanded, ANIMATION_DURATION);
//        this.hideContentFragment();
    }



    public void expand(boolean requestFocus) {
        this.mCollapsedHeight = this.getHeight();
        this.toggleToolbar(true);
//        if (this.mBackgroundTransition != null) {
//            this.mBackgroundTransition.startTransition(ANIMATION_DURATION);
//        }

        this.mIsExpanded = true;
//        this.animateStates(true, 1.0F, 0.0F);
//        Utils.crossFadeViews(this.mExpanded, this.mCollapsed, ANIMATION_DURATION);
        if (requestFocus) {
            this.mSearchEditText.requestFocus();
        }

    }

    public void handle(QMUITopBar toolbar) {
        this.mTopBar = toolbar;
    }
}
